﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Common;

namespace Catering.UserTraining.SlrCompanyTrainning
{
    public partial class staff_lessons_test : System.Web.UI.Page
    {
        public int i_Number = 0;
        public DataTable dt_PurchaseInfo = new DataTable();
        public int m_LessonID = 0;
        public string strStaffID = "";
        public string strStaffName = "";
        public string strStaffNodesID = "";
        public string strStaffNodesName = "";
        public bool bShowAns = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            m_LessonID = CommRequest.GetInt("LessionID", 0);
            if (!this.InitUserInfo()) return;
            if (!IsPostBack)
            {
                this.InitPageInfo();
                this.LoadRep();
            }
        }

        public bool InitUserInfo()
        {
            if (Session["StaffID"] != null)
            {
                strStaffID = Session["StaffID"].ToString();
                strStaffName = Session["StaffName"].ToString();
                strStaffNodesID = Session["StaffNodesID"].ToString();
                strStaffNodesName = Session["StaffNodesName"].ToString();
                return true;
            }
            return false;
        }

        public void InitPageInfo()
        {
            this.lblLessionName.Text = new Catering_Rules.StaffLessons().GetModel(m_LessonID).LessonName;
            DataTable dt_TestList = new Catering_Rules.StaffLessonsTests().ViewGetList(" 1=1 AND LessonID=" + m_LessonID + " ").Tables[0];//分页处理
            this.ddl_Test.DataSource = dt_TestList;
            this.ddl_Test.DataTextField = "TestName";
            this.ddl_Test.DataValueField = "TestID";
            this.ddl_Test.DataBind();
            this.lbl_Points.Text = "得分:";
            this.ddl_Test.Enabled = false;
            this.btn_Search.Visible = false;
            this.btn_Save.Visible = false;

            if (dt_TestList != null && dt_TestList.Rows.Count > 0)
            {
                Catering_Model.StaffLessonsLog oTemp = new Catering_Rules.StaffLessonsLog().GetModel(m_LessonID, int.Parse(strStaffNodesID), int.Parse(strStaffID));
                if (oTemp == null || oTemp.TestID == null || oTemp.TestID < 0)
                {
                    this.ddl_Test.Items[0].Selected = true;
                    this.ddl_Test.Enabled = true;
                    this.btn_Search.Visible = true;
                    this.btn_Save.Visible = true;
                    this.lbl_Points.Text = "";
                }
                else
                {
                    this.bShowAns = true;
                    this.ddl_Test.SelectedValue = oTemp.TestID.ToString();
                    this.lbl_Points.Text = "得分" + oTemp.TestPoints.ToString();
                }

                //this.lbl_StafInfo.Text = "(" + strStaffName + "[" + strStaffID + "][" + strStaffNodesName + "])";
                //this.lbtn_Exit.Visible = true;
                //this.StaffLogin.Visible = false;
            }
        }

        //绑定Rep控件
        public void LoadRep()
        {
            string strwhere = " 1=1 ";
            if (this.ddl_Test.SelectedItem != null)
            {
                strwhere += " AND TestID=" + this.ddl_Test.SelectedValue + " ";
                this.hf_TestID.Value = this.ddl_Test.SelectedValue;
                this.hf_TestName.Value = this.ddl_Test.SelectedItem.Text;
            }
            else
            {
                strwhere += " AND TestID=0 ";
            }
            dt_PurchaseInfo = new Catering_Rules.TestsQueList().GetList(strwhere).Tables[0];
            ViewState["Test_" + m_LessonID + "_" + strStaffID] = dt_PurchaseInfo;
            this.Repeater1.DataSource = dt_PurchaseInfo;
            this.Repeater1.DataBind();

        }

        protected void btn_Search_Click(object sender, EventArgs e)
        {
            this.LoadRep();
        }

        protected void btn_Return_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrCompanyTrainning/staff_lessons_list.aspx";
            Response.Redirect(url);
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (ViewState["Test_" + m_LessonID + "_" + strStaffID] == null) return;
            DataTable dt_QueList = new DataTable();
            dt_QueList = (DataTable)ViewState["Test_" + m_LessonID + "_" + strStaffID];
            decimal dcmSum = 0;
            for (int i = 0; i < dt_QueList.Rows.Count; i++)
            {
                if (this.Repeater1.Items.Count >= i)
                {
                    string queans = dt_QueList.Rows[i]["QueAnswer"].ToString();
                    string userans = "";
                    if (dt_QueList.Rows[i]["QueType"].ToString() == "S")
                    {
                        RadioButton rb_a = this.Repeater1.Items[i].FindControl("R_A") as RadioButton;
                        RadioButton rb_b = this.Repeater1.Items[i].FindControl("R_B") as RadioButton;
                        RadioButton rb_c = this.Repeater1.Items[i].FindControl("R_C") as RadioButton;
                        RadioButton rb_d = this.Repeater1.Items[i].FindControl("R_D") as RadioButton;
                        if (rb_a.Checked) userans = "A|";
                        if (rb_b.Checked) userans = "B|";
                        if (rb_c.Checked) userans = "C|";
                        if (rb_d.Checked) userans = "D|";
                    }
                    else
                    {
                        CheckBox cbo_a = this.Repeater1.Items[i].FindControl("C_A") as CheckBox;
                        CheckBox cbo_b = this.Repeater1.Items[i].FindControl("C_B") as CheckBox;
                        CheckBox cbo_c = this.Repeater1.Items[i].FindControl("C_C") as CheckBox;
                        CheckBox cbo_d = this.Repeater1.Items[i].FindControl("C_D") as CheckBox;
                        if (cbo_a.Checked) userans += "A|";
                        if (cbo_b.Checked) userans += "B|";
                        if (cbo_c.Checked) userans += "C|";
                        if (cbo_d.Checked) userans += "D|";
                    }
                    Catering_Model.StaffTestQueLog _ModelStaffTestQueLog = new Catering_Model.StaffTestQueLog();
                    _ModelStaffTestQueLog.TestID = int.Parse(this.hf_TestID.Value);
                    _ModelStaffTestQueLog.TestName = this.hf_TestName.Value;
                    _ModelStaffTestQueLog.AddTime = DateTime.Now;
                    _ModelStaffTestQueLog.QueAnswer = queans;
                    _ModelStaffTestQueLog.UserAnswer = userans;
                    _ModelStaffTestQueLog.QueContentA = dt_QueList.Rows[i]["QueContentA"].ToString();
                    _ModelStaffTestQueLog.QueContentB = dt_QueList.Rows[i]["QueContentB"].ToString();
                    _ModelStaffTestQueLog.QueContentC = dt_QueList.Rows[i]["QueContentC"].ToString();
                    _ModelStaffTestQueLog.QueContentD = dt_QueList.Rows[i]["QueContentD"].ToString();
                    _ModelStaffTestQueLog.QueID = int.Parse(dt_QueList.Rows[i]["QueID"].ToString());
                    _ModelStaffTestQueLog.QueName = dt_QueList.Rows[i]["QueName"].ToString();
                    _ModelStaffTestQueLog.QueNO = dt_QueList.Rows[i]["QueNO"].ToString();
                    _ModelStaffTestQueLog.QuePoints = 100 / this.Repeater1.Items.Count;
                    _ModelStaffTestQueLog.QueType = dt_QueList.Rows[i]["QueType"].ToString();
                    _ModelStaffTestQueLog.UserID = int.Parse(strStaffID);
                    _ModelStaffTestQueLog.UserPoints = queans == userans ? _ModelStaffTestQueLog.QuePoints : 0;
                    new Catering_Rules.StaffTestQueLog().Add(_ModelStaffTestQueLog);
                    dcmSum += _ModelStaffTestQueLog.UserPoints;
                }
            }

            Catering_Model.StaffLessonsLog _ModelStaffLessonsLog = new Catering_Model.StaffLessonsLog();
            _ModelStaffLessonsLog = new Catering_Rules.StaffLessonsLog().GetModel(m_LessonID, int.Parse(strStaffNodesID), int.Parse(strStaffID));
            if (_ModelStaffLessonsLog != null)
            {
                _ModelStaffLessonsLog.TestID = int.Parse(this.hf_TestID.Value);
                _ModelStaffLessonsLog.TestPoints = Convert.ToDecimal(dcmSum.ToString("0.00"));
                _ModelStaffLessonsLog.TestDate = DateTime.Now;
                new Catering_Rules.StaffLessonsLog().Update(_ModelStaffLessonsLog);
                ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('保存成功！')", true);
            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (!bShowAns) return;
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                HiddenField lbtnQueTyp = e.Item.FindControl("hf_QueType") as HiddenField;
                HiddenField lbtnQueID = e.Item.FindControl("hf_QueID") as HiddenField;
                Catering_Model.StaffTestQueLog _ModelStaffTestQueLog = new Catering_Model.StaffTestQueLog();
                _ModelStaffTestQueLog = new Catering_Rules.StaffTestQueLog().GetModel(int.Parse(this.hf_TestID.Value), int.Parse(strStaffID), int.Parse(lbtnQueID.Value));
                if (_ModelStaffTestQueLog == null) return;
                if (lbtnQueTyp.Value == "S")
                {
                    switch (_ModelStaffTestQueLog.UserAnswer)
                    {
                        case "A|":
                            RadioButton rb_a = e.Item.FindControl("R_A") as RadioButton;
                            rb_a.Checked = true;
                            break;
                        case "B|":
                            RadioButton rb_b = e.Item.FindControl("R_B") as RadioButton;
                            rb_b.Checked = true;
                            break;
                        case "C|":
                            RadioButton rb_c = e.Item.FindControl("R_C") as RadioButton;
                            rb_c.Checked = true;
                            break;
                        default:
                            RadioButton rb_d = e.Item.FindControl("R_D") as RadioButton;
                            rb_d.Checked = true;
                            break;
                    }
                }
                else
                {
                    CheckBox cbo_a = e.Item.FindControl("C_A") as CheckBox;
                    CheckBox cbo_b = e.Item.FindControl("C_B") as CheckBox;
                    CheckBox cbo_c = e.Item.FindControl("C_C") as CheckBox;
                    CheckBox cbo_d = e.Item.FindControl("C_D") as CheckBox;
                    if (_ModelStaffTestQueLog.UserAnswer.Contains("A|")) cbo_a.Checked = true;
                    if (_ModelStaffTestQueLog.UserAnswer.Contains("B|")) cbo_b.Checked = true;
                    if (_ModelStaffTestQueLog.UserAnswer.Contains("C|")) cbo_c.Checked = true;
                    if (_ModelStaffTestQueLog.UserAnswer.Contains("D|")) cbo_d.Checked = true;
                }
            }
        }

        public string GetAns(string strQueID)
        {
            if (!bShowAns) return "";
            Catering_Model.TestsQueList _ModelTestsQueList = new Catering_Rules.TestsQueList().GetModel(int.Parse(strQueID));
            if (_ModelTestsQueList != null) return "正确答案："+_ModelTestsQueList.QueAnswer.Replace("|", "");
            return "";
        }
    }
}